package com.my.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.domain.kplunion.OrderService.request.query.OrderRowReq;
import com.jd.open.api.sdk.domain.kplunion.OrderService.response.query.OrderRowResp;
import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
import com.my.dto.MessageDTO;
import com.my.response.WecomGroupDetailResponse;
import com.my.result.MessageResult;
import com.my.service.MessageService;
import com.my.service.WecomRobotService;
import com.my.util.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author sjx
 * @date 2025/2/11 9:42
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private WecomRobotService wecomRobotService;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void receiveMsg(MessageDTO messageDTO) {
        //从京东联盟中获取数据
        /*DefaultJdClient defaultJdClient = new DefaultJdClient("https://api.jd.com/routerjson",null,"29defa3b4e2bf401d54942a8d45d8c23","a1f6fca5b4c748f9bc42cadffdba12f6");
        UnionOpenOrderRowQueryRequest unionOpenOrderRowQueryRequest = new UnionOpenOrderRowQueryRequest();
        OrderRowReq orderReq = new OrderRowReq();
        orderReq.setOrderId(Long.parseLong(messageDTO.getContent()));
        unionOpenOrderRowQueryRequest.setOrderReq(orderReq);

        UnionOpenOrderRowQueryResponse unionOpenOrderRowQueryResponse;
        try{
            unionOpenOrderRowQueryResponse = defaultJdClient.execute(unionOpenOrderRowQueryRequest);
        } catch (Exception e){
            log.error("调用京东联盟数据错误",e);
            return;
        }

        if(unionOpenOrderRowQueryResponse.getQueryResult().getData() == null || unionOpenOrderRowQueryResponse.getQueryResult().getData().length < 1){
            log.error("没有调用出任何订单信息,当前订单号:{}",messageDTO.getContent());
            return;
        }
        OrderRowResp orderRowResp = unionOpenOrderRowQueryResponse.getQueryResult().getData()[0];*/

        //消息整理并推送到企业微信中
        sendMsg("发送成功",messageDTO.getFromUserName(),messageDTO.getAgentId());
    }

    private void sendMsg(String content,String fromUserName,String agentid){

        String accessToken = AccessTokenUtil.getToken();

        String msgType = "text";

        JSONObject paramJson = new JSONObject();
        paramJson.put("touser",fromUserName);
        paramJson.put("msgtype",msgType);
        paramJson.put("content",content);
        paramJson.put("agentid",agentid);

        ParameterizedTypeReference<MessageResult> result = new ParameterizedTypeReference<MessageResult>() {};
        restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken, HttpMethod.POST,new HttpEntity<>(paramJson),result);

    }

}
